package com.moyun.zbmy.zizhou.ui.adapter.holder;

import android.content.Context;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.moyun.zbmy.zizhou.R;
import com.moyun.zbmy.zizhou.beans.FCityItem;
import com.moyun.zbmy.zizhou.beans.GovItemBean;
import com.moyun.zbmy.zizhou.beans.NewsItem;
import com.moyun.zbmy.zizhou.ui.activities.MoreServiceActivity;
import com.moyun.zbmy.zizhou.ui.utils.GlideUtil;
import com.ruihang.generalibrary.utils.ListUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class NormalServiceHolder extends BaseAbsHolder<GovItemBean>{
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_more)
    TextView tvMore;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private Context context;
    private GridLayoutManager gridLayoutManager;
    private NormalServiceAdapter adapter;
    public NormalServiceHolder(Context context, View itemView,OnServiceItemClick itemClick) {
        super(context, itemView);
        this.context=context;
        this.serviceItemClick=itemClick;
        ButterKnife.bind(this, itemView);
    }

    @Override
    public void setData(final GovItemBean govItemBean) {
         tvName.setText(govItemBean.getInstitutionName());
         tvMore.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 if (internalClick != null) {
                     if(serviceItemClick!=null){
                         serviceItemClick.OnItemClick(govItemBean);
                     }
                     internalClick.onItemInternalClick(itemView, v, getAdapterPosition());
                 }
             }
         });
        tvMore.setVisibility(View.GONE);
         if(ListUtils.isListValued(govItemBean.getInfoList())){
             if(govItemBean.getInfoList().size()>4){
                 tvMore.setVisibility(View.VISIBLE);
             }else{
                 tvMore.setVisibility(View.GONE);
             }
             recyclerView.setVisibility(View.VISIBLE);
             adapter=new NormalServiceAdapter(govItemBean.getInfoList());
             gridLayoutManager = new GridLayoutManager(context, 2);
             recyclerView.setItemAnimator(new DefaultItemAnimator());
             recyclerView.setLayoutManager(gridLayoutManager);
             recyclerView.setAdapter(adapter);
         }else {
             recyclerView.setVisibility(View.GONE);
         }
    }

    private class NormalServiceAdapter extends RecyclerView.Adapter<ServiceItemHolder>{
        private List<NewsItem> data;

        public NormalServiceAdapter(List<NewsItem> data) {
            this.data = data;
//            if(ListUtils.isListValued(data)){
//                if(data.size()>4){
//                    for(int i=data.size()-1;i>3;i--){
//                        data.remove(i);
//                    }
//                }
//            }
        }

        @Override
        public ServiceItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(context).inflate(R.layout.item_service, parent, false);
            ServiceItemHolder  bah = new ServiceItemHolder(v);
            return bah;
        }

        @Override
        public void onBindViewHolder(ServiceItemHolder holder, int position) {
//            if(position<=3) {
//
//            }
            final NewsItem item = data.get(position);
            holder.name.setText(item.getNewsTitle());
            holder.name.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (serviceItemClick != null) {
                        serviceItemClick.OnServiceItemClick(item);
                    }
                }
            });


            GlideUtil.getGlideWith11DefNoFit(context, item.getNewsImage()).into(holder.img);
            holder.img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(serviceItemClick!=null){
                        serviceItemClick.OnServiceItemClick(item);
                    }
                }
            });


        }

        @Override
        public int getItemCount() {
            if(data.size()>4){
                return 4;
            }
            return data.size();
        }
    }
    public class ServiceItemHolder extends RecyclerView.ViewHolder{
        public TextView name;
        public ImageView img;

        public LinearLayout linearLayout;
        public ServiceItemHolder(View itemView) {
            super(itemView);
            name=itemView.findViewById(R.id.tv_name);
            img=itemView.findViewById(R.id.iv_img);
            linearLayout=itemView.findViewById(R.id.ll);
        }

    }

    public OnServiceItemClick getServiceItemClick() {
        return serviceItemClick;
    }

    public void setServiceItemClick(OnServiceItemClick serviceItemClick) {
        this.serviceItemClick = serviceItemClick;
    }

    private OnServiceItemClick serviceItemClick;
    public interface OnServiceItemClick{
        void OnServiceItemClick(NewsItem item);
        void OnItemClick(GovItemBean govItemBean);
    }

}
